<?php
/**
 * welcome developer.
 * author: liangbo
 * company:tencent suzhou
 * contact:173120209
 * mobile:18550451730
 * Date: 2016-12-17
 * Time: 10:50
 */

class core {

    // 运行程序
    function run()
    {
        spl_autoload_register(array($this, 'loadClass'));
        new load();
        $this->setReporting();
        $this->removeMagicQuotes();
        $this->unregisterGlobals();
        $this->Route();
    }

    // 路由处理
    function route()
    {
        $controllerName = default_class;
        $action = default_action;
        $root = $_SERVER['SCRIPT_NAME'];
        $request = $_SERVER['REQUEST_URI'];
        $URI = array();
        $url = trim(str_replace($root, '', $request), '/');
        if (!empty($url)) {
            $urlArray = explode('/', $url);
            // 获取控制器名
            $controllerName = $urlArray[0];
            if($this->is_controller($controllerName))
            {

                $controllerName = $urlArray[0];
                array_shift($urlArray);

            }else{

                $controllerName = default_class;

            }
            // 获取动作名

            $action = empty($urlArray[0]) ? 'index' : $urlArray[0];
            if((int)method_exists($controllerName.'Controller',$action))
            {


              $action=empty($urlArray[0]) ? 'index' : $urlArray[0];
                array_shift($urlArray);

            }else{


                $action=default_action;

            }
            //获取URL参数
            $queryString=str_replace('.'.HTML_PREFIX,'',$urlArray);
            if(isset($queryString[0]))
            {

                $queryString=explode('-',$queryString[0]);

            }

            $queryString = empty($queryString) ? array() : $queryString;
        }

        // 数据为空的处理
        $queryString  = empty($queryString) ? array() : $queryString;
        /*var_dump($queryString );*/
       /* var_dump($queryString);*/
        // 实例化控制器
        $controller = $controllerName . 'Controller';
        $dispatch = new $controller($controllerName, $action);

        // 如果控制器存和动作存在，这调用并传入URL参数
        if ((int)method_exists($controller, $action)) {
            call_user_func_array(array($dispatch, $action), $queryString);
        } else {
            exit($controller . "控制器不存在");
        }
    }

    // 检测开发环境
    function setReporting()
    {
        if (APP_DEBUG === true) {
            error_reporting(E_ALL);
            ini_set('display_errors','On');
        } else {
            error_reporting(E_ALL);
            ini_set('display_errors','Off');
            ini_set('log_errors', 'On');
            ini_set('error_log', RUNTIME_PATH. 'logs/error.log');
        }
    }

    // 删除敏感字符
    function stripSlashesDeep($value)
    {
        $value = is_array($value) ? array_map('stripSlashesDeep', $value) : stripslashes($value);
        return $value;
    }

    // 检测敏感字符并删除
    function removeMagicQuotes()
    {
        if ( get_magic_quotes_gpc()) {
            $_GET = stripSlashesDeep($_GET );
            $_POST = stripSlashesDeep($_POST );
            $_COOKIE = stripSlashesDeep($_COOKIE);
            $_SESSION = stripSlashesDeep($_SESSION);
        }
    }

    // 检测自定义全局变量（register globals）并移除
    function unregisterGlobals()
    {
        if (ini_get('register_globals')) {
            $array = array('_SESSION', '_POST', '_GET', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES');
            foreach ($array as $value) {
                foreach ($GLOBALS[$value] as $key => $var) {
                    if ($var === $GLOBALS[$key]) {
                        unset($GLOBALS[$key]);
                    }
                }
            }
        }
    }

    // 自动加载控制器和模型类
    static function loadClass($class)
    {
        $frameworks = TENCENT_PATH . $class . '.php';
        $controllers = APP_PATH . 'application/controllers/' . $class . '.php';
        $models = APP_PATH . 'application/models/' . $class . '.php';

        if (file_exists($frameworks)) {
            // 加载框架核心类
            include $frameworks;
        } elseif (file_exists($controllers)) {
            // 加载应用控制器类
            include $controllers;
        } elseif (file_exists($models)) {
            //加载应用模型类
            include $models;
        } else {
            /* 错误代码 */
        }
    }

    function is_controller($class)
    {


        $class=strtolower($class);

        $controllers = APP_PATH . 'application/controllers/' . $class . 'Controller.php';
        if(file_exists($controllers))
        {


          return true;


        }else{

            return false;

        }


    }










}